package com.xx.job.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.job.controller.dto.PageDTO;
import com.xx.job.entity.system.SysFileDir;
import com.xx.job.mapper.SysFileDirMapper;
import com.xx.job.service.system.SysFileDirService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangcy
 * @since 2021-01-14
 */
@Service
public class SysFileDirServiceImpl extends ServiceImpl<SysFileDirMapper, SysFileDir> implements SysFileDirService {

    @Autowired
    private SysFileDirMapper sysFileDirMapper;

    @Override
    public SysFileDir add(SysFileDir sysFileDir) {
         sysFileDirMapper.insert(sysFileDir);
        return sysFileDir;
    }

    @Override
    public int delete(Long id) {
        return sysFileDirMapper.deleteById(id);
    }

    @Override
    public int update(SysFileDir sysFileDir) {
        return sysFileDirMapper.updateById(sysFileDir);
    }

    @Override
    public SysFileDir select(Long id) {
        return sysFileDirMapper.selectById(id);
    }

    @Override
    public IPage<SysFileDir> selectPage(PageDTO pageDTO, SysFileDir sysFileDir) {
        IPage<SysFileDir> page = new Page<>(pageDTO.getPageIndex(),pageDTO.getPageSize());
        return sysFileDirMapper.selectPage(page,null);
    }

    @Override
    public List<Map<String, Object>> selectAll() {

        return geFileDirList(0l);
    }

    private  List<Map<String,Object>> geFileDirList(Long pId){
        List<Map<String,Object>> list = new ArrayList<>();
        QueryWrapper<SysFileDir> wrapper = new QueryWrapper<>();
        wrapper.eq("p_id",pId);

        List<SysFileDir> sysFileDirs = sysFileDirMapper.selectList(wrapper);
        if(sysFileDirs != null && sysFileDirs.size() != 0){
            for (SysFileDir sysFileDir : sysFileDirs) {
                Map<String,Object> data = new HashMap<>();
                data.put("id",sysFileDir.getId());
                data.put("label",sysFileDir.getLabel());
                data.put("children",geFileDirList(sysFileDir.getId()));
                list.add(data);
            }
        }
        return list;
    }
}
